package cn.tannn.cat.file.server.controller.manage;

import cn.tannn.cat.file.api.entity.FileIndexVO;
import cn.tannn.cat.file.server.controller.manage.dto.FileIndexMetaPage;
import cn.tannn.cat.file.server.controller.manage.dto.FileIndexMetaTransfer;
import cn.tannn.cat.file.server.entity.FileIndexMeta;
import cn.tannn.cat.file.server.service.FileIndexMetaService;
import cn.tannn.cat.file.server.service.FileOperateService;
import cn.tannn.jdevelops.annotations.web.mapping.PathRestController;
import cn.tannn.jdevelops.jpa.result.JpaPageResult;
import cn.tannn.jdevelops.result.response.ResultPageVO;
import cn.tannn.jdevelops.result.response.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 文件索引数据管理
 *
 * @author tnnn
 * @version V1.0
 * @date 2024-03-09 19:28
 */
@Tag(name = "文件索引数据管理", description = "文件索引数据管理")
@PathRestController("fileIndex")
@RequiredArgsConstructor
@Slf4j
public class FileIndexMetaController {

    private final FileIndexMetaService fileIndexMetaService;
    private final FileOperateService fileOperateService;

    @Operation(summary = "查询文件元数据-分页")
    @PostMapping("page")
    public ResultPageVO<FileIndexVO,JpaPageResult<FileIndexVO>> page(@RequestBody @Valid FileIndexMetaPage page) {
        Page<FileIndexMeta> files = fileIndexMetaService.findPage(page, page.getPage());
        JpaPageResult<FileIndexVO> filesResult = JpaPageResult.toPage(files, FileIndexVO.class);
        return ResultPageVO.success(filesResult, "查询成功");
    }

    @GetMapping(value = "download")
    @Operation(summary = "下载文件")
    @Parameter(name = "fileIndexId", description = "存储索引ID", required = true)
    public void download(@RequestParam("fileIndexId") Long fileIndexId, HttpServletResponse response) {
        fileOperateService.download(fileIndexId, response);
    }


    @Operation(summary = "批量删除文件元数据")
    @PostMapping("deletes")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<String> deletes(@RequestBody List<Long> ids) {
        List<FileIndexMeta> fileStorages = fileIndexMetaService.findByIds(ids);
        // 1. 删除真实存储文件
        // 2. 删除文件记录、
        fileStorages.forEach(fileOperateService::remove);
        return ResultVO.successMessage("存储信息和真实源文件删除成功");
    }


    @Operation(summary = "转移存储器", description = "比如将本地存储转到minio存储[人工确定转存的桶是否存在]")
    @PostMapping("transfer")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<String> transfer(@RequestBody @Valid FileIndexMetaTransfer transfer) {
        // 1. 查询原来的上传记录
//        fileIndexMetaService.findOnly("id", transfer.getId()).ifPresent(fileStorage -> {
//            // 2. 根据上传记录的数据拿到文件
//            // 3. todo 将文件上传的新的存储器
//            // 4. todo 删除原来的存储文件
//            // 5. todo 删除原来的文件记录
//            fileIndexMetaService.getJpaBasicsDao().delete(fileStorage);
//        });
//        return ResultVO.successMessage("转存成功");
        return ResultVO.successMessage("开发中");
    }

}
